package model.contents;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import threads.DownloadQueue;
import util.LoggingUtils;
import util.PlayerUtilities;

public class Content {
	private int			id;
	private ContentType	type;
	private String		path;
	private boolean		hasUpdate;
	private int			server;

	public Content(int id, ContentType type, String path, boolean hasUpdate, int server) {
		this.id = id;
		this.type = type;
		this.path = path;
		this.hasUpdate = hasUpdate;
		this.server = server;
	}

	public static List<Content> getContentListFromCSVFile(String fileName) {
		List<Content> contentList = new ArrayList<Content>();
		if (!new File(fileName).exists()) {
			return contentList;
		}
		List<String[]> csvData = PlayerUtilities.getArrayFromCSVFile(fileName);
		String[] fields = csvData.remove(0);

		for (String[] contentData : csvData) {
			int id = 0;
			ContentType type = ContentType.OTHER;
			String path = "";
			boolean hasUpdate = false;
			int server = 0;
			boolean isValidContent = false;

			// loop through each row and fetch Content data
			for (int i = 0; i < fields.length; i++) {
				try {
					String field = fields[i];
					if (field.equals("content_id")) {
						id = Integer.parseInt(contentData[i]);
						isValidContent = true;
					} else if (field.equals("content_type")) {
						type = ContentType.getContentType(contentData[i]);
					} else if (field.equals("content")) {
						path = contentData[i];
					} else if (field.equals("has_update")) {
						hasUpdate = contentData[i].equals("1") ? true : false;
					} else if (field.equals("server")) {
						server = Integer.parseInt(contentData[i]);
					} else {
						System.err.println("invalid content field: " + field);
					}
				} catch (NumberFormatException ex) {
					isValidContent = false;
					System.out.println("invalid data format from source file.");
					ex.printStackTrace();
					
					String classMethod = "model.contents .getContentListFromCSVFile()";
					LoggingUtils.logStart(classMethod);
					LoggingUtils.log(Level.SEVERE,ex.getMessage()+" invalid data format from source file." + classMethod,ex);
				}
				catch(ArrayIndexOutOfBoundsException ax)
				{
					String classMethod = "model.contents .getContentListFromCSVFile()";
					LoggingUtils.logStart(classMethod);
					LoggingUtils.log(Level.SEVERE,ax.getMessage()+ classMethod,ax);
				}
				catch(NullPointerException nu)
				{
					String classMethod = "model.contents .getContentListFromCSVFile()";
					LoggingUtils.logStart(classMethod);
					LoggingUtils.log(Level.SEVERE,nu.getMessage()+ classMethod,nu);
				}
			}

			if (isValidContent && type != null) {
				Content c = new Content(id, type, path, hasUpdate, server);
				contentList.add(c);
			} else {
				System.err.println("can't create this content");
			}
		}
		return contentList;
	}

	/**
	 * @return the hasUpdate
	 */
	public boolean hasUpdate() {
		return this.hasUpdate;
	}

	/**
	 * @param hasUpdate
	 *            the hasUpdate to set
	 */
	public synchronized void setHasUpdate(boolean hasUpdate) {
		this.hasUpdate = hasUpdate;
		if(this.hasUpdate()) {
			DownloadQueue.addTaskToDownloadQueue(this);
		}
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return this.id;
	}

	/**
	 * @return the type
	 */
	public ContentType getType() {
		return this.type;
	}

	/**
	 * @return the path
	 */
	public String getPath() {
		return this.path;
	}

	/**
	 * @return the server
	 */
	public int getServer() {
		return this.server;
	}

	@Override
	public String toString() {
		return String.format("%s(%s): %s", this.type.toString(), this.getId(), this.getPath());
	}

	/**
	 * get the remote path to download the content file
	 * 
	 * @return the remote file path of the content file for downloading purposes
	 */
	public String getRemotePath() {
		String path = null;
		switch (this.getServer()) {
			case 0:
				path = PlayerUtilities.getRemoteImageDirectoryForCurrentBox() + this.getPath();
				break;
			case 1:
				path = "http://www.digitalmarketingbox.com/" + this.getPath();
				break;
			case 2:
				path = "http://www.digitalrealestatebox.com/" + this.getPath();
				break;
			// by default, all contents are rooted at dmb.com
			default:
				path = "http://www.digitalmarketingbox.com/" + this.getPath();
				break;
		}
		return path;
	}

	/**
	 * returns a file path that can be used to access the local content file
	 * 
	 * @return the local file path of the content
	 */
	public String getLocalPath() {
		File f = new File(this.path);
		return this.type.getPath() + f.getName();
	}
	
	
	
	public String getLocalhtmlPath() {
		if(this.path.contains("http://"))
		{
			return this.path;
		}
		File f = new File(this.path);
		return this.type.getPath() + f.getName();
	}

	public boolean isValidContent() {
		return this.type!=ContentType.OTHER && new File(this.getLocalPath()).exists();
	}
	
	public void delete() {
		
		System.out.println("FOR DELETING --- "+this.getLocalPath());
		File contentFile = new File(this.getLocalPath());
		if(contentFile.exists()) {
			contentFile.delete();
		}
		else
		{
			System.out.println("FOR DELETING --- File not exists");
		}
	}
}